<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>js实现单例模式</title>
    </head>
    <body>
        <script>
            let userModule = (function (params) {
                let time = Date.now();
                function query() {
                    console.log('query', time);
                }
                function submit() {
                    console.log('submit', time);
                }
                return { query, submit };
            })();

            userModule.query();
            userModule.submit();

            let mySingleton = (function () {
                let instance;
                function mySingleton(params) {
                    instance = instance || (this instanceof mySingleton ? this : new mySingleton(params));
                    instance.init(params);
                    return instance;
                }
                mySingleton.prototype.init = function (params) {
                    this.params = params;
                };
                return mySingleton;
            })();
        </script>
    </body>
</html>
